<!DOCTYPE html>
<html>
<head>
  <title>Leaflet.DistortableImage Example</title>
  <meta charset="utf-8" />

  <meta name="viewport" content="width=device-width, initial-scale=1.0">

  <script src="../node_modules/leaflet/dist/leaflet-src.js" type="text/javascript" charset="utf-8"></script>
  <link href="../node_modules/font-awesome/css/font-awesome.min.css" rel="stylesheet">
  <link rel="stylesheet" href="../node_modules/leaflet/dist/leaflet.css" type="text/css" media="screen" title="no title" charset="utf-8">
  <script src="../node_modules/leaflet-toolbar/dist/leaflet.toolbar.js"></script>
  <link href="../node_modules/leaflet-toolbar/dist/leaflet.toolbar.css" rel="stylesheet"/>

  <!-- for full-res export -->
  <script src="../node_modules/jquery/dist/jquery.js"></script>
  <script src="../node_modules/webgl-distort/dist/webgl-distort.js"></script>
  <script src="../node_modules/glfx-js/dist/glfx.js"></script>

  <link rel="stylesheet" href="../dist/leaflet.distortableimage.css" type="text/css" media="screen" title="no title" charset="utf-8">
  <script src="../dist/leaflet.distortableimage.js"></script>
</head>
<body style="margin:0;">

  <form id="test_form" >
    <input type="file" id="inputimage" accept="image/*">
  </form>

  <div id="map" style="width:100%; height:100%; position:absolute; top:0;"></div>

  <script>

    var map

    (function(){

      // basic Leaflet map setup
      map = new L.map('map').setView([51.505, -0.09], 13);
      L.tileLayer('https://{s}.tiles.mapbox.com/v3/anishshah101.ipm9j6em/{z}/{x}/{y}.png', {
        maxZoom: 18,
        attribution: 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, ' +
          '<a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, ' +
          'Imagery © <a href="http://mapbox.com">Mapbox</a>',
        id: 'examples.map-i86knfo3'
      }).addTo(map);

      images = []
      select = function(e){
        for (var i in images) {
          img = this
          if (img._leaflet_id != images[i]._leaflet_id) {
            /* Deselect (disable) other images */
            images[i].editing.disable()
            /* Ensure that other toolbars are removed */
            if (images[i].editing.toolbar) {
              map.removeLayer(images[i].editing.toolbar);
            }
          }
        }
        /* Ensure this is enabled */
        this.editing.enable()
        /* If it's locked, allow event to propagate on to map below */
        if (this.editing._mode != "lock") e.stopPropagation()
      }

      // create an image
      img = new L.DistortableImageOverlay(
        'example.jpg', {
          corners: [
            new L.latLng(51.52,-0.10),
            new L.latLng(51.52,-0.14),
            new L.latLng(51.50,-0.10),
            new L.latLng(51.50,-0.14)
          ],
          mode: 'lock'
        }
      ).addTo(map);
      images.push(img)
      L.DomEvent.on(img._image, 'click', select, img);

      // create a second image
      img = new L.DistortableImageOverlay(
        'example.jpg', {
          corners: [
            new L.latLng(51.51,-0.10),
            new L.latLng(51.51,-0.14),
            new L.latLng(51.49,-0.11),
            new L.latLng(51.49,-0.15)
          ]
        }
      ).addTo(map);
      images.push(img)

      L.DomEvent.on(img._image, 'load', img.editing.enable, img.editing);
      L.DomEvent.on(img._image, 'click', select, img);

      // this doesn't work -- it triggers when clicking toolbar
      // can we stopPropagation of toolbar clicks? is that impolite.
      map.on('mousedown', function() {
        for (var i in images) {
          /* Deselect (disable) all images */
          //images[i].editing.disable()
          /* Remove toolbars */
          if (images[i].editing.toolbar) {
            //map.removeLayer(images[i].editing.toolbar);
          }
        }
      });

    })();
  </script>
      
</html>
